package com.zxbit.flash.base;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zxbit.flash.R;
import com.zxbit.flash.view.dialog.LoadingDialog;

import crossoverone.statuslib.StatusUtil;

public abstract class BaseTitleActivity extends BaseActivity {
    private FrameLayout content_container;
    private ImageView back_btn;
    private RelativeLayout title_view;
    private TextView text_title;
    private TextView text_right;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init(savedInstanceState);
        // 设置沉浸式状态栏
        setSystemInvadeBlack();
        initTitleView();
        initViews();
        onUIReady();
    }

    protected void init(Bundle savedInstanceState) {}

    protected void setSystemInvadeBlack() {
        StatusUtil.setUseStatusBarColor(this, Color.parseColor("#00000000"));
        // 第二个参数是是否沉浸,第三个参数是状态栏字体是否为黑色。
        StatusUtil.setSystemStatus(this, true, false);
    }

    private void initTitleView() {
        int resId = getResId();
        if (getPageTitle() != null) {
            setContentView(resId);
            title_view = findViewById(R.id.title_view);
            back_btn = findViewById(R.id.back_btn);
            text_title = findViewById(R.id.text_title);
            text_right = findViewById(R.id.text_right);
            content_container = findViewById(R.id.content_container);
            LayoutInflater.from(getApplicationContext()).inflate(getContentResId(), content_container);
            processTitleView();
        } else {
            setContentView(getContentResId());
        }
    }

    private void processTitleView() {
        if (back_btn != null) {
            back_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }

        if (text_title != null) {
            text_title.setText(getPageTitle());
            // 第二个参数是是否沉浸,第三个参数是状态栏字体是否为黑色。
            StatusUtil.setUseStatusBarColor(this, getResources().getColor(R.color.colorWhite));
            StatusUtil.setSystemStatus(this, false, true);
        }
    }

    protected abstract void initViews();

    protected abstract void onUIReady();

    public String getPageTitle(){
        return null;
    }

    protected void setBackVisiable(boolean status) {
        if (back_btn != null) {
            if (status)
                back_btn.setVisibility(View.VISIBLE);
            else
                back_btn.setVisibility(View.GONE);
        }
    }

    protected void setRightText(String text) {
        if (text_right != null) {
            text_right.setText(text);
        }
    }

    /**
     * 设置标题
     * @param text
     */
    protected void setTitleText(String text) {
        if (text_title != null) {
            text_title.setText(text);
        }
    }

    protected TextView getRightView(){
        return text_right;
    }

    @Override
    protected int getResId() {
        return R.layout.layout_of_base_content;
    }

    @Override
    protected ILoading createLoadingView() {
        return new LoadingDialog(this, R.style.loading_dialog);
    }

    protected abstract int getContentResId();

    /**
     * 通过资源id 获取字符串
     *
     * @param strResourceId
     * @return
     */
    protected String getStringResource(int strResourceId) {
        return getResources().getString(strResourceId);
    }

    /**
     * 隐藏键盘三连
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideKeyboard(v, ev)) {
                hideKeyboard(v.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击EditText的事件，忽略它。
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false;
    }

    private void hideKeyboard(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
